package org.spolo.android.util;

import java.util.ArrayList;

import org.spolo.android.bean.ActionParameter;
import org.spolo.android.enventmanager.EventHandlerInterface;
import org.spolo.android.nodetree.Vcanvas;

import android.view.MotionEvent;

public class McMove implements EventHandlerInterface {

	private int cf = 0;
	
	private float lastPointX = 0;
	private float firstOneX = 0;

	@Override
	public Object execute(Object parameter) {
		ActionParameter ap = (ActionParameter) parameter;
		ArrayList<MotionEvent> eventlist = ap.getEvents();
//		MotionEvent m1 = eventlist.get(0);
//		MotionEvent m2 = eventlist.get(1);
		float oneX = eventlist.get(0).getX();
		float twoX = eventlist.get(1).getX();
		Vcanvas vc =  ap.getVcanvas();
		if(vc == null || !(vc instanceof MovieClip)){
			System.out.println("没有获取到是哪个movieclip触发的该事件");
			return null;
		}
		MovieClip mc = (MovieClip) vc;
		float dx = 0;
		if(firstOneX != oneX){
			lastPointX = 0;
			firstOneX = oneX;
		}
		if(lastPointX != 0){
			dx = twoX - lastPointX;
		}else{
			dx = twoX - oneX;
		}
		lastPointX = twoX;
		if (dx < 0) {
			// 向左移动
			cf = mc.currentFrame();
			mc.gotoFrame(cf - 1);
		} else if(dx > 0){
			// 向右移动
			cf = mc.currentFrame();
			mc.gotoFrame(cf + 1);
		}
		return null;
	}

}
